package com.gnerv.cms.utils;

import com.gnerv.cms.freemarker.tag.ListTagDirective;
import freemarker.template.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;
import org.springframework.util.ClassUtils;
import org.springframework.util.FileCopyUtils;
import org.springframework.util.ObjectUtils;

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Slf4j
public class FreemarkerUtils {

    public static String createHtml(String templatePath, String outPath, Map<String, Object> map, String fileName) throws Exception {
        //创建fm的配置
        Configuration configuration = new Configuration(Configuration.getVersion());
        //指定默认编码格式
        configuration.setDefaultEncoding("UTF-8");

        Template template = null;
        try {
            template = configuration.getTemplate(templatePath);
        } catch (IOException e) {
            log.info("获取模板异常", e);
        }
        String content = "";
        try {
            content = FreeMarkerTemplateUtils.processTemplateIntoString(template, null);
            return content;
        } catch (IOException e) {
            log.info("生成内容异常", e);
        } catch (TemplateException e) {
            log.info("生成内容异常", e);
        }

        File file = new File(outPath);
        if (!file.exists() && !file.isDirectory()) {
            //创建多级文件夹
            file.mkdirs();
        }
        FileOutputStream fileOutputStream = null;
        try {
            fileOutputStream = new FileOutputStream(new File(file, fileName));
            FileCopyUtils.copy(content.getBytes(), fileOutputStream);
        } catch (FileNotFoundException e) {
            log.info("生成静态html异常", e);
        } catch (IOException e) {
            log.info("生成静态html异常", e);
        }
        return content;
    }

    public static String getParamToString(String name, Map<String, TemplateModel> paramMap) {
        TemplateModel param = paramMap.get(name);
        if (ObjectUtils.isEmpty(param)) {
            return "";
        }
        if (param instanceof TemplateScalarModel) {
            try {
                return ((TemplateScalarModel) param).getAsString();
            } catch (TemplateModelException e) {
                log.info("获取参数失败！", e);
            }
        }
        return "";
    }

    public static Object getParamToList(String name, Map<String, TemplateModel> paramMap) {
        TemplateModel param = paramMap.get(name);
        if (ObjectUtils.isEmpty(param)) {
            return "";
        }
        if (param instanceof DefaultListAdapter) {
            return ((DefaultListAdapter) param).getWrappedObject();
        }
        if (param instanceof DefaultArrayAdapter) {
            return ((DefaultArrayAdapter) param).getWrappedObject();
        }
        if (param instanceof SimpleSequence) {
            try {
                return ((SimpleSequence) param).toList();
            } catch (TemplateModelException e) {
                e.printStackTrace();
            }
        }
        return new ArrayList<>();
    }


}
